package com.example.demo.controller.Document;

import com.example.demo.entity.ChargePoi;
import com.example.demo.entity.Shop;
import com.example.demo.model.ResponseData;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("/document")
public class InsertDocumentController {

    @Autowired
    MongoTemplate mongoTemplate;

    private static final String COMMON_NAME="shops";

    @ApiOperation(value="对集合中插入数据",notes="插入数据")
    @PostMapping("/insert")
    public ResponseData insert() throws IOException, JSONException {

        Shop shop=new Shop();
        shop.setName("铅笔");
        shop.setPrice(12);
        shop.setType("学习用品");
        shop.setNumber(10);
        shop.setRemarks("测试数据");
        Shop shop1=mongoTemplate.insert(shop,"shops");
        return ResponseData.success(shop1);
    }

    @ApiOperation(value="添加多条数据",notes = "添加数据")
    @PostMapping("/insertAll")
    public ResponseData insertAll(){
        Shop shop=new Shop();
        shop.setName("格尺");
        shop.setPrice(121);
        shop.setType("学习用品");
        shop.setNumber(101);
        shop.setRemarks("多测试测试数据11");

        Shop shop1=new Shop();
        shop1.setName("圆规");
        shop1.setPrice(121);
        shop1.setType("学习用品");
        shop1.setNumber(101);
        shop1.setRemarks("多测试测试数据11");

        List<Shop> shopList=new ArrayList<>();
        shopList.add(shop);
        shopList.add(shop1);

        List<Shop> shopList1=mongoTemplate.insert(shopList);
        return ResponseData.success(shopList1);
    }

    /**
     * save地理空间数据
     */
    @ApiOperation(value="保存地理空间数据",notes="保存数据")
    @PostMapping("/test1")
    public void test1() {
        for (int x = 100; x < 131; x++) {
            for (int y = 30; y < 61; y++) {
                Double loca[] = new Double[]{Double.valueOf(x), Double.valueOf(y)};
                ChargePoi chargePoi = new ChargePoi();
                chargePoi.setPoi_id("poiid" + x);
                chargePoi.setMedia_url("http://www.baidu.com?params=" + x);
                chargePoi.setPoi_name("vini" + Arrays.toString(loca));
                chargePoi.setPrice(Math.random() * 100);
                chargePoi.setLocation(loca);
                mongoTemplate.insert(chargePoi);
            }
        }
    }

    /**
     * near地理空间数据
     */
    @ApiOperation(value="对集合中保存数据尽心测试",notes="测试数据")
    @GetMapping("/test7")
    public ResponseData test7() {
        NearQuery query = NearQuery.near(12,12).maxDistance(new Distance(100000, Metrics.KILOMETERS)).num(2L);
        GeoResults<ChargePoi> result = mongoTemplate.geoNear(query, ChargePoi.class);
        return ResponseData.success(result);
    }


    /**
     * 将实体POJO转化为JSON
     * @param obj
     * @return
     * @throws JSONException
     * @throws IOException
     */
    public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        // Convert object to JSON string
        String jsonStr = "";
        try {
            jsonStr =  mapper.writeValueAsString(obj);
        } catch (IOException e) {
            throw e;
        }
        return new JSONObject(jsonStr);
    }
    /**
     * 将json转化为实体POJO
     * @param jsonStr
     * @param obj
     * @return
     */
    public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
        T t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            t = objectMapper.readValue(jsonStr,
                    obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }


}
